package com.oak.broke.model.vip.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oak.broke.model.vip.service.ICommonFileService;
import com.oak.broke.utils.FileUtil;
import com.oak.broke.model.vip.dao.CommonFileDao;
import com.oak.broke.model.vip.dto.BaseFileInfoDTO;
import com.oak.broke.model.vip.dto.CommonFileDTO;
import com.oak.broke.model.vip.entity.CommonFileEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tanghan
 * @Description 文件serviceImpl
 * @date 2023/04/18/ 10:21:00
 */
@Service
public class CommonFileServiceImpl extends ServiceImpl<CommonFileDao, CommonFileEntity> implements ICommonFileService {

    @Autowired
    private FileUtil fileUtil;

    @Override
    public void saveFile(List<CommonFileDTO> fileList, BaseFileInfoDTO baseFileInfo) {
        if (CollectionUtils.isEmpty(fileList)) {
            return;
        }
        List<CommonFileEntity> saveFileList = fileList.stream().map(file -> {
            CommonFileEntity commonFile = new CommonFileEntity();
            BeanUtils.copyProperties(baseFileInfo, commonFile);
            commonFile.setFileName(file.getFileName());
            commonFile.setFilePath(fileUtil.moveFile(file.getFilePath()));
//            commonFile.setCreateTime(new Date());
//            commonFile.setUpdateTime(new Date());
            return commonFile;
        }).collect(Collectors.toList());
        saveBatch(saveFileList);
    }
}
